#include "sound_pipeline.h"

sound_pipeline * sound_pipeline_instance = NULL;

sound_pipeline * sound_pipeline::self(){
	if (!sound_pipeline_instance)
		sound_pipeline_instance = new sound_pipeline;
	return sound_pipeline_instance;
}

sound_pipeline::sound_pipeline (){

	if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 ){ 
		debug("neotvorilo sa audio %s",Mix_GetError());
		return; 
	} 
	
	FOR(i,MUSIC_LEN)
		music[i]=NULL;
	FOR(i,SOUND_LEN)
		sound[i]=NULL;
	
	ifmute=1;
	music_volume=0.5;
	sound_volume=0.5;

	if ((music[MUSIC_0] = Mix_LoadMUS( "./menu0.wav" ))==NULL) debug("Nenacital sa zvuk");
	
}

void sound_pipeline::_mute(int _ifmute){
	if (_ifmute==0){
		ifmute=0;
		Mix_ResumeMusic();
	}
	else{
		ifmute=1;
		Mix_PauseMusic();
	}
}

void sound_pipeline::_set_music_volume(float volume){
	music_volume=volume;
	if (music_volume<0)
		music_volume=0;
	if (music_volume>1)
		music_volume=1;
	Mix_VolumeMusic(music_volume);
}

void sound_pipeline::_set_sound_volume(float volume){
	sound_volume=volume;
	if (sound_volume<0)
		sound_volume=0;
	if (sound_volume>1)
		sound_volume=1;
}

void sound_pipeline::_playM(int type){
	if (Mix_PlayMusic(music[type], -1)==-1)
		debug("Pustas nenahrate veci %s",Mix_GetError());	
	if (ifmute==1)
		Mix_PauseMusic();
	
}

void sound_pipeline::_playS(int type,float volume,float x, float y){
	if (ifmute)
		return;
	if( Mix_PlayChannel( type, sound[type], 0 ) == -1 ){ 
		debug("nejde prehrat %s",Mix_GetError()); 
	} 
	if (Mix_SetPosition( type, 180/M_PI*atan2(y,x),sqrt(x*x + y*y) )==-1)
		debug("SetPosition problem %s",Mix_GetError());
}







